package Music_Main;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import MusicPathdeal.CreateFile;
import MusicPathdeal.MusicLeadin;
import Music_View.music_view;
import list.MusicList;
import list.ThreadList;
import model.Model;
import model.Music;
import service.Player;
import service.Setting;
import util.DirInput;
import util.FileInput;
import util.List_File;
import util.musicPlay;


public class music_main {
	public static int Id=0;
	static music_view frame;
	static Player Newplaying=null;
	private static FileInput fileinput;
	private static DirInput dirInput;
	public static void main(String[] args) throws ClassNotFoundException, IOException { 
        frame = new music_view();//创建程序窗口  
        frame.setUndecorated(true);
        frame.setVisible(true);//放在添加组件后面执行
        CreateFile.Creatfile();
        frame.table.setModel(new Model());
    }
	
	
	public static void openmusic() {
		//添加MP3文件
		fileinput = new FileInput(frame);
    	fileinput.open();
    	File[] s=fileinput.getFiles();
    	ArrayList<Music> musiclist=MusicList.getList();
    	if(s!=null){
    	    for(int i=0;i<s.length;i++){
    	    Music music= new Music();
        	music.setId(musiclist.size()+"");
        	music.setName(s[i].getName().substring(0,s[i].getName().lastIndexOf(".")));
        	music.setPath(s[i].getAbsolutePath());
            for(int y=0;y<musiclist.size();y++) {
	           if(s[i].getName().substring(0,s[i].getName().lastIndexOf(".")).equals(musiclist.get(y).getName())) {
	        	   System.out.println("fdf");	 
	        	   JOptionPane.showMessageDialog(null, musiclist.get(y).getName()+"已存在！", "标题",JOptionPane.ERROR_MESSAGE); 
	        	   return;
	        	   }
	           }
    	     musiclist.add(music);
    	    frame.table.setModel(new Model());
    	    }
    	}
    	   frame.getTable().setSelectionBackground(Color.green);
	}
	

	public static void openmusicpath() {
		// 添加MP3文件夹
	    dirInput = new DirInput(frame);
		dirInput.open();
    	File s=dirInput.getFile();
    	if(s!=null){	
    	    addmusic(s.getAbsolutePath());
    	    }
    	   frame.getTable().setSelectionBackground(Color.green);
	}

	public static void exit() throws IOException {
		 //退出
		if(ThreadList.getList().size()!=0) {
		ThreadList.getList().get(0).stopplay();
		ThreadList.getList().clear();
		}
		if (MusicList.getList().size()!=0) {
			System.out.println("写入文件");
			  MusicLeadin.writePath();
			//清空之前的内容
//			FileList.clear("file/musiclist.txt");
//			ArrayList<Music> list=MusicList.getList();
//			for (int i = 0; i < list.size(); i++) {
//				FileList.writeFile("file/musiclist.txt",list.get(i).getId()+","+list.get(i).getName()+","
//						+list.get(i).getPath()+"\n");
//			}
			
		}
		  System.exit(0);
	}
	public static void MinWindow() {
		// TODO 自动生成的方法存根
		frame.setExtendedState(JFrame.ICONIFIED);
	}
	public static void PanelFullScree(boolean IS) {
		// TODO 自动生成的方法存根
		if(IS) {
		     frame.setExtendedState(Frame.MAXIMIZED_BOTH);
		     frame.getBigorsmall().setIcon(new ImageIcon(music_view.class.getResource("/images/changesmall.png")));
		}else {
			frame.setExtendedState(Frame.NORMAL);
			frame.getBigorsmall().setIcon(new ImageIcon(music_view.class.getResource("/images/changebig.png")));
		}
	}
	
	
private static void addmusic(String path) {//增加mp3文件夹
		System.out.println("增加mp3文件夹");
		ArrayList<Music> musiclist=MusicList.getList();
			
		    List_File fm = new List_File();
	        ArrayList<String[]> FileList = fm.serachFiles(path);
	        
	        for (int i = 0; i < FileList.size(); i++) {
	        	    boolean temp=true;
	        	    Music music= new Music();
	        	    String[] s=(String[]) FileList.get(i);
	        	    for(int y=0;y<musiclist.size();y++) {
	        	      if(s[0].substring(0,s[0].lastIndexOf(".")).equals(musiclist.get(y).getName())) {
		        	      temp=false;
		        	      JOptionPane.showMessageDialog(null, musiclist.get(y).getName()+"已存在！", "标题",JOptionPane.ERROR_MESSAGE); 
		        	      break;
	        	      }
	        	    }
	        	    if(temp) {
			        	music.setId(musiclist.size()+"");
			        	music.setName(s[0].substring(0,s[0].lastIndexOf(".")));
			        	music.setPath(s[1]);
		        	    musiclist.add(music);
	        	    }
			     }
	        
				frame.getTable().setModel(new Model());
				frame.getTable().setSelectionBackground(Color.green);
	}


public static void PlayOrPause() {
	// TODO 自动生成的方法存根
	if(MusicList.getList().size()!=0) {
	if(Newplaying==null) {
		Newplaying =new Player(frame.getSlider(),frame.getProgress());
		Id=0;
		Newplaying.setMusic(MusicList.getList().get(Id));
		frame.getTable().setRowSelectionInterval(0,0);
		ThreadList.add(Newplaying);
		musicPlay.Start();
	}else {
		if (ThreadList.getList().size()!=0) {
			Newplaying=ThreadList.getList().get(0);
		}
		Newplaying.Pause();
	}
	}
	frame.getTable().setSelectionBackground(Color.green);
}

public static void Before() {
	// 上一首

	if(Newplaying==null&&MusicList.getList().size()!=0) {
		Newplaying =new Player(frame.getSlider(),frame.getProgress());
		Id=0;
		Newplaying.setMusic(MusicList.getList().get(Id));
		frame.getTable().setRowSelectionInterval(0,0);
		ThreadList.add(Newplaying);
		musicPlay.Start();
	}else {
	System.out.println("上一首");
	ArrayList<Player> list=ThreadList.getList();
	Id=Integer.parseInt(list.get(0).getMusic().getId());
	list.get(0).stopplay();
	list.clear();
	Newplaying =new Player(frame.getSlider(),frame.getProgress());
	if(Id!=0){ 
		if(Id == 1) {
			Id=0;
			Newplaying.setMusic(MusicList.getList().get(Id));
			frame.getTable().setRowSelectionInterval(0,0);
		}else {
			frame.getTable().setRowSelectionInterval(Id-2,Id-1); 
			Id=Id-1;
			Newplaying.setMusic(MusicList.getList().get(Id));
		}
    }else {
    	Newplaying.setMusic(MusicList.getList().get(MusicList.getList().size()-1));
		frame.getTable().setRowSelectionInterval(MusicList.getList().size()-1,MusicList.getList().size()-1);
    }
	frame.getTable().setSelectionBackground(Color.green);
	ThreadList.add(Newplaying);
	musicPlay.Start();
	System.out.println(Id);
	}
}



public static void Next() {
	// 下一首
	if(Newplaying==null&&MusicList.getList().size()!=0) {
		Newplaying =new Player(frame.getSlider(),frame.getProgress());
		Id=0;
		Newplaying.setMusic(MusicList.getList().get(Id));
		frame.getTable().setRowSelectionInterval(0,0);
		ThreadList.add(Newplaying);
		musicPlay.Start();
	}else {
	System.out.println("下一首");
	ArrayList<Player> list=ThreadList.getList();
	list.get(0).stopplay();
	list.clear();
	Newplaying =new Player(frame.getSlider(),frame.getProgress());
	if(Id!=MusicList.getList().size()-1){ //122
	frame.getTable().setRowSelectionInterval(Id,Id+1);  //123条
	Id=Id+1;
	Newplaying.setMusic(MusicList.getList().get(Id));
    }else {
    	Id=0;
		Newplaying.setMusic(MusicList.getList().get(Id));
		frame.getTable().setRowSelectionInterval(0,0);
    }
	frame.getTable().setSelectionBackground(Color.green);
	System.out.println(Id);
	ThreadList.add(Newplaying);
	musicPlay.Start();
	}
}




public static void Unmute(boolean voice, float temp) {
	// TODO 自动生成的方法存根
	 if(voice) {
		 frame.slider.setValue((int) temp);
		  setVol(temp);
	  }else {
		  frame.slider.setValue(-80);
		  setVol(-80);
	  }
}


public static void setVol(float value) {
	System.out.println(value);
	if (ThreadList.getList().size()!=0) {
	if(value==-80) {
	  frame.getVoicelabel().setIcon(new ImageIcon(music_view.class.getResource("/images/mediaUnmute.png")));
	}else frame.getVoicelabel().setIcon(new ImageIcon(music_view.class.getResource("/images/mediaMute.png")));
	ThreadList.getList().get(0).getVolume().setValue(value);
	String string1, string2;
	string1 = "0";
	string2 = String.valueOf((int)(100*(value+80)/86));
	if (value == 6) {
		string2 = "00";
		string1 = "1";
	} else if (value < -73.4) {
		string1 = "00";
	}
	frame.getVoiceaccout1label().setText(string1);
	frame.getVoiceaccout2label().setText(string2);
}
}


public static void playerloop() {
	// TODO 自动生成的方法存根
    if (ThreadList.getList().size()!=0) {
    	Newplaying=ThreadList.getList().get(0);
	  }else {
		  Newplaying=new Player(frame.getSlider(),frame.getProgress());
		  ThreadList.getList().add(Newplaying);
	  }
       
      String[] s={"default","rand","onecircle","morecircle","one"};
      
	  Setting.setMode(s[frame.getJBox().getSelectedIndex()]);
}


//public static void jumpTo(float f) {
//	// TODO 自动生成的方法存根
//	System.out.println(f);
//	Newplaying.getJSliderPlayProgress().setValue((int)(f*Newplaying.getTime()));
//	System.out.println(Newplaying.getJSliderPlayProgress().getValue());
//	musicPlay.Start();
//}


public static void Delet() {
	//删除
	int rowNum = frame.getTable().getSelectedRow();
	MusicList.getList().remove(rowNum);
	for(int i=rowNum;i<MusicList.getList().size();i++) {
		MusicList.getList().get(i).setId(String.valueOf(i)+"");
	}
	frame.getTable().setModel(new Model());
	ArrayList<Player> list=ThreadList.getList();
//	if(-1!=Player.currentid) {
//		Id=Player.currentid;
//	}
	 if (list.size()!=0 && MusicList.getList().size()!=0) {
		 if(rowNum == Id) {
			 if(Id!=0) {
				 Id = rowNum-1;
			     frame.getTable().setRowSelectionInterval(Id,Id);
			 }
			 else {
				 Id = MusicList.getList().size()-1;
				 frame.getTable().setRowSelectionInterval(Id,Id);
			 }
			 Next();
		 }
	  }else {
		  if(list.size()!=0) {
		    list.get(0).stopplay();
			list.clear();
		  }
	  }
	 frame.getTable().setRowSelectionInterval(Id,Id);
}



 public static void Clear() {
	// 清空
		MusicList.getList().clear();
		frame.getTable().setModel(new Model());
		ArrayList<Player> list=ThreadList.getList();
		if(ThreadList.getList().size()!=0) {
		list.get(0).stopplay();
		list.clear();
		}
		MusicLeadin.Clear();
}
 
	public static Player getNewplaying() {
		// TODO 自动生成的方法存根
		return Newplaying;
	}


	public static void ClickList() {
		// 点击列表播放
		Id = frame.getTable().getSelectedRow();
		frame.getTable().setRowSelectionInterval(Id, Id); 
		if(Id == -1) {
			JOptionPane.showMessageDialog(frame, "你没有选择一项");
			return;
		}
		 ArrayList<Player> list=ThreadList.getList();
		  Newplaying=new Player(frame.getSlider(),frame.getProgress());
		  Newplaying.setMusic(MusicList.getList().get(Id));
		 if (list.size()==0) {
			  ThreadList.add(Newplaying);
	      }else{
			  list.get(0).stopplay();
			  list.clear();
		      list.add(Newplaying);
		}
		   frame.getTable().setSelectionBackground(Color.green);
		    musicPlay.Start();
	}


}
